package io.zhifei.modules.sys.system.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.zhifei.common.utils.PageUtils;
import io.zhifei.common.utils.ToolUtils;
import io.zhifei.modules.sys.system.dao.SignUpDao;
import io.zhifei.modules.sys.system.entity.SignUpEntity;
import io.zhifei.modules.sys.system.form.SignUpParam;
import io.zhifei.modules.sys.system.form.SignUpResult;
import io.zhifei.modules.sys.system.form.SysUserResult;
import io.zhifei.modules.sys.system.form.customer.CustomerParam;
import io.zhifei.modules.sys.system.service.SignUpService;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 客户报名 服务实现类
 * </p>
 *
 * @author duke
 * @since 2020-12-17
 */
@Service
public class SignUpServiceImpl extends ServiceImpl<SignUpDao, SignUpEntity> implements SignUpService {

    @Override
    public void add(SignUpParam param) {
        SignUpEntity entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(SignUpParam param) {
        this.removeById(getKey(param));
    }

    @Override
    public void update(SignUpParam param) {
        SignUpEntity oldEntity = getOldEntity(param);
        SignUpEntity newEntity = getEntity(param);
        ToolUtils.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public SignUpResult getDetail(SignUpParam param) {
        return this.baseMapper.getDetail(param);
    }

    @Override
    public PageUtils getList(Page page, SignUpParam param) {
        IPage pageList = this.baseMapper.getList(page,param);
        return new PageUtils(pageList);
    }

    @Override
    public PageUtils getCustomerPageList(Page page, CustomerParam param) {
        IPage pageList = this.baseMapper.getCustomerPageList(page,param);
        return new PageUtils(pageList);
    }

    @Override
    public List<SysUserResult> getUserList() {
        return this.baseMapper.getUserList();
    }

    private Serializable getKey(SignUpParam param) {
        return param.getId();
    }

    private SignUpEntity getOldEntity(SignUpParam param) {
        return this.getById(getKey(param));
    }

    private SignUpEntity getEntity(SignUpParam param) {
        SignUpEntity entity = new SignUpEntity();
        ToolUtils.copyProperties(param, entity);
        return entity;
    }

}
